package cn.dengta.webapp.user.model;

import cn.dengta.common.model.BizFlag;
import cn.dengta.common.model.ValueProxy;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.context.model.WesKit;
import lombok.Getter;
import me.codeplayer.util.Cmp;
import me.codeplayer.util.X;

/**
 * 用户资料完善度分值
 */
@Getter
public enum UserInfoSupply implements LabelI18nProxy<UserInfoSupply, Integer> {

	/** 头像 */
	AVATAR(0, "头像", 30, 30, 1),
	/** 相册 */
	PHOTO(1, "相册", 20, 0, 1 << 1),
	/** 昵称 */
	NICKNAME(2, "昵称", 0, 0, 1 << 2),
	/** 国家 */
	COUNTRY(3, "国家", 10, 0, 1 << 3),
	/** 魅力标签 */
	LIKE_TAG(4, "魅力标签", 10, 15, 1 << 4),
	/** 个人介绍 */
	SLOGAN(5, "个人介绍", 10, 10, 1 << 5),
	/** 相册视频 */
	PHOTO_VIDEO(6, "相册视频", 20, 12, 1 << 6, 1),
	/** 基本资料 */
	INFO(7, "基本资料", 0, 15, 1 << 7),
	/** 相册旅行照 国内版 */
	PHOTO_TOUR(8, "相册旅行照-国内版", 0, 8, 1 << 8, 1),
	/** 相册生活照 国内版 */
	PHOTO_LIVE(9, "相册生活照-国内版", 0, 10, 1 << 9, 2),
	//

	;

	// 定义私有变量
	public final Integer value;
	// 国外版 分值
	public final Integer score;
	/** 国内版分值 */
	public final Integer interScore;
	/** 国内版 标记下标 */
	public final Integer interTagIndex;
	public final long flag;
	final ValueProxy<UserInfoSupply, Integer> proxy;

	UserInfoSupply(Integer value, String label, Integer score, Integer interScore, long flag) {
		this(value, label, score, interScore, flag, null);
	}

	// 构造函数，枚举类型只能为私有
	UserInfoSupply(Integer value, String label, Integer score, Integer interScore, long flag, Integer interTagIndex) {
		this.value = value;
		this.score = score;
		this.interScore = interScore;
		this.interTagIndex = interTagIndex;
		this.flag = flag;
		this.proxy = new ValueProxy<>(this, value, label);
	}

	public static UserInfoSupply[] getValues() {
		return ValueProxy.getCachedArray(UserInfoSupply.class, UserInfoSupply::values);
	}

	public static int calcScore(WesKit wesKit, final long flags) {
		int score = 0;
		if (flags > score) {
			UserInfoSupply[] values = getValues();
			for (UserInfoSupply value : values) {
				if (value.hasFlag(flags)) {
					score += wesKit.isInterior() ? value.getInterScore() : value.getScore();
				}
			}
		}
		return score;
	}

	public boolean hasScore() {
		return score != null;
	}

	public boolean hasFlag(final long flags) {
		return BizFlag.hasFlag(flags, flag);
	}

	public boolean canToggle(final long flags, boolean addOrRemove) {
		return X.matchAllOrNone(addOrRemove, !hasFlag(flags));
	}

	public long toggleFlag(final long flags, boolean addOrRemove) {
		return BizFlag.WritableBizFlag.toggleFlag(flags, flag, addOrRemove);
	}

	public boolean hasInfoFlag(WesKit wesKit) {
		return wesKit.isInterior() ? Cmp.gt(this.interScore, 0) : Cmp.gt(this.score, 0);
	}

	public static UserInfoSupply of(Integer value) {
		return AVATAR.getValueOf(value);
	}

}
